package br.fatec.ads.servlet;

import java.io.IOException;
import java.text.ParseException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.fatec.ads.dao.PerguntaDAO;
import br.fatec.ads.exception.DataBaseException;
import br.fatec.ads.exception.PerguntaException;
import br.fatec.ads.model.Pergunta;

public class PerguntaAlterar extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public PerguntaAlterar() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			HttpSession session = request.getSession(false);
			
			Pergunta pergunta = new Pergunta();
			PerguntaDAO perguntaDAO = new PerguntaDAO();
		
			pergunta = perguntaDAO.getPergunta(Integer.parseInt(request.getParameter("idPerguntaAlterar")));
			
			session.setAttribute("pergunta", pergunta);
			 
			request.getRequestDispatcher("cadpergunta_a.jsp").forward(request, response);
		} catch (PerguntaException e) {
			e.printStackTrace();
		} catch (DataBaseException e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			HttpSession session = request.getSession(false);
			Pergunta pergunta = new Pergunta();
			
			pergunta.setIdPergunta(Integer.parseInt(request.getParameter("idPerguntaAlterar")));
			pergunta.setObrigatorio(request.getParameter("obrigatorio") == null ? "N" : "S");
			pergunta.setPadrao(request.getParameter("padrao") == null ? "N" : "S");
			pergunta.setUsuario(session.getAttribute("usuarioDesc").toString());
			
			PerguntaDAO perguntaDAO = new PerguntaDAO();
		
			perguntaDAO.alterarPergunta(pergunta);
			
	        request.getRequestDispatcher("PerguntaPesquisar?acao=SUCESSO_ALTERAR").forward(request, response);
		} catch (PerguntaException e) {
			request.getRequestDispatcher("cadprocessoSeletivo_a.jsp?acao=erro").forward(request, response);
			e.printStackTrace();
		} catch (DataBaseException e) {
			request.getRequestDispatcher("cadprocessoSeletivo_a.jsp?acao=erro").forward(request, response);
			e.printStackTrace();
		}
	}

}
